﻿/*
 * Programer: JR Padfield
 * Description: This class does some general game operations.
 * Version: 1
 * Date: 12/17/13 * 
 */

using System;
using System.Text;
using System.Security.Cryptography;
using Microsoft.Xna.Framework;

namespace TheWorld.Global
{
    public class General
    {

        #region Constructor and Singleton

        // Private Constructor
        private General()
        {
        }

        // Singleton Accessor
        static General instance;
        public static General Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new General();
                }

                return instance;
            }
        }

        #endregion

        #region Fields


        #endregion

        // Change Sprite //
        public void changeSprite()
        {
            int SpriteCount = 5;

            if (Static.rSprite >= SpriteCount)
            {
                Static.rSprite = 1;
            }
            else
            {
                Static.rSprite = Static.rSprite + 1;
            }

        }

        ///////////////////////
        // Menu Alert System //
        ///////////////////////
        public void AlertMsg(string Caption, int Colour)
        {
            Static.menuAlertColour = Colour;
            Static.menuAlertMessage = Caption;
            Static.menuAlertTimer = (int)Static._gameTime.TotalGameTime.TotalMilliseconds + 3000;
        }

        //////////////////////
        // String Functions //
        //////////////////////

        // Check Username & Password for 3.No Chars //
        public bool isLoginLegal(String Username, String Password)
        {
            if (Username.Length > 2) { if (Password.Length > 2) { return true; } }
            return false;
        }

        // Check Username & Password for 3.No Chars //
        public bool isRegisterLegal(String Username, String Password)
        {
            if (Username.Length > 2) { if (Password.Length > 2) { return true; } }
            return false;
        }

        // MD5 Password //
        public string CalculateMD5Hash(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }

        // String Width //
        public int getStringWidth(int Font, string text)
        {
            GfxManager GfxM = GfxManager.Instance;
            Vector2 value = GfxM.FONT_TEXTURE[Font].MeasureString(text);
            return (int)value.X;
        }

        // String Height //
        public int getStringHeight(int Font, string text)
        {
            GfxManager GfxM = GfxManager.Instance;
            Vector2 value = GfxM.FONT_TEXTURE[Font].MeasureString(text);
            return (int)value.Y;
        }
    }
}
